package com.android.kotlincoroutinemvvmdemo.util;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import androidx.appcompat.widget.AppCompatImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import jp.wasabeef.glide.transformations.BlurTransformation;

public class FastBlurUtil3 {

    private FastBlurUtil3() {}

    public static void getBlurBackgroundDrawer(Activity activity, AppCompatImageView imageView, int radius) {
        Bitmap bmp = takeScreenShot(activity);
        Glide.with(activity)
                .load(bmp)
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(radius, 2)))
                .into(imageView);
    }

    private static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap drawingCache = view.getDrawingCache();

        //获取屏幕长和高
        int width = activity.getResources().getDisplayMetrics().widthPixels;
        int height = activity.getResources().getDisplayMetrics().heightPixels;

        Bitmap bmp = Bitmap.createBitmap(drawingCache, 0, 0, width, height);
        view.destroyDrawingCache();
        return bmp;
    }
}
